package com.lovely602.ai.alibaba.controller.tool;


import com.lovely602.ai.alibaba.tools.RecruitServiceFunction;
import com.lovely602.ai.alibaba.tools.TimeTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.tool.function.FunctionToolCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 *
 * @author lizhixing
 */
@Slf4j
@RestController
@RequestMapping(value = "tool")
public class ToolController {

    @Autowired
    private ChatModel chatModel;

    @Autowired
    private ChatClient chatClient;

    /**
     * 计算器服务
     */
    @GetMapping(value = "/calc", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
    public String calc(@RequestParam(value = "msg", defaultValue = "请计算1+1") String msg) {
        return ChatClient.builder(chatModel)
                .build()
                .prompt()
                .system("""
                        您是算术计算器的代理。
                        您能够支持加法运算、乘法运算等操作，其余功能将在后续版本中添加，如果用户问的问题不支持请告知详情。
                        在提供加法运算、乘法运算等操作之前，您必须从用户处获取如下信息：两个数字，运算类型。
                        请调用自定义函数执行加法运算、乘法运算。
                        请讲中文。
                        """)
                .user(msg)
                .toolNames("addOperation", "mulOperation")
                .call()
                .content();
    }

    /**
     * 时间服务
     */
    @GetMapping(value = "/time", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
    public String time(@RequestParam(value = "msg", defaultValue = "获取北京时间") String msg) {
        return ChatClient.builder(chatModel)
                .build()
                .prompt()
                .user(msg)
                .tools(new TimeTool())
                // .toolContext(Map.of("userId", "123456")) // 传递工具上下文
                .call()
                .content();
    }

    /**
     * 招聘小助手
     */
    @GetMapping(value = "/recruit", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
    public String recruit(@RequestParam(value = "msg", defaultValue = "张三是否有面试资格") String msg) {

        FunctionToolCallback<RecruitServiceFunction.Request, RecruitServiceFunction.Response> functionToolCallback = FunctionToolCallback
                .builder("recruitServiceFunction", new RecruitServiceFunction())
                .description("招聘小助手")
                .inputType(RecruitServiceFunction.Request.class)
                .build();

        return ChatClient.builder(chatModel)
                .build()
                .prompt()
                .user(msg)
                .toolCallbacks(functionToolCallback)
                .call()
                .content();
    }

}
